package vns.server;

import java.util.ArrayList;
import java.util.List;
import javax.jdo.PersistenceManager;
import javax.jdo.Query;

import vns.client.service.ObjectPropertyTypeService;
import vns.server.domain.ObjectPropertyType;
import vns.server.domain.ObjectValue;
import vns.shared.ObjectPropertyTypeDTO;

import com.google.appengine.api.datastore.Key;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;

public class ObjectPropertyTypeServiceImpl extends RemoteServiceServlet
		implements ObjectPropertyTypeService {

	@Override
	public ObjectPropertyTypeDTO Create(ObjectPropertyTypeDTO optypeDTO) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		ObjectPropertyType action = null;
		try {
			action = new ObjectPropertyType(optypeDTO);
			pm.makePersistent(action);
		} finally {
			pm.close();
		}
		return action.toDTO();
	}

	@Override
	public ObjectPropertyTypeDTO Load(Key id) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		ObjectPropertyTypeDTO actionDTO = null;
		try {
			ObjectPropertyType action = pm.getObjectById(
					ObjectPropertyType.class, id);
			actionDTO = action.toDTO();
		} finally {
			pm.close();
		}
		return actionDTO;
	}

	@Override
	public ObjectPropertyTypeDTO Update(ObjectPropertyTypeDTO optypeDTO) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		ObjectPropertyType action = null;
		// ObjectPropertyType attach = null;
		try {
			action = pm.getObjectById(ObjectPropertyType.class,
					optypeDTO.getId());
			action.updateFromDTO(optypeDTO);
			// attach = pm.makePersistent(action);
		} finally {
			pm.close();
		}

		return action.toDTO();
	}

	@Override
	public boolean Remove(Key id) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try {
			ObjectPropertyType action = pm.getObjectById(
					ObjectPropertyType.class, id);
			if (action != null) {
				pm.deletePersistent(pm.getObjectById(ObjectPropertyType.class,
						id));
			}
		} finally {
			pm.close();
		}
		return true;
	}

	@Override
	public ArrayList<ObjectPropertyTypeDTO> Loadall() {
		ArrayList<ObjectPropertyTypeDTO> listactionDTO = new ArrayList<ObjectPropertyTypeDTO>();

		PersistenceManager pm = PMF.get().getPersistenceManager();
		Query q = pm.newQuery(ObjectPropertyType.class);
		try {
			@SuppressWarnings("unchecked")
			List<ObjectPropertyType> res = (List<ObjectPropertyType>) q
					.execute();
			for (ObjectPropertyType actiontype : res) {
				ObjectPropertyType term = pm.detachCopy(actiontype);
				listactionDTO.add(term.toDTO());
			}
		} finally {
			pm.close();
		}
		return listactionDTO;
	}

	private boolean containInList(ArrayList<ObjectPropertyTypeDTO> list,
			ObjectPropertyTypeDTO element) {
		if (element.getId() == null)
			return false;
		for (ObjectPropertyTypeDTO optDTO : list) {
			if (optDTO.getId().equals(element.getId()))
				return true;
		}
		return false;
	}

	public void UpdateAll(ArrayList<ObjectPropertyTypeDTO> listNew) {

		ArrayList<ObjectPropertyTypeDTO> listOld = this.Loadall();
		for (ObjectPropertyTypeDTO element : listNew) {
			if (!containInList(listOld, element)) {
				element = this.Create(element);
			} else {
				this.Update(element);
			}
		}
		for (ObjectPropertyTypeDTO element : listOld) {
			if (!containInList(listNew, element)) {
				this.Remove(element.getId());
			}
		}
	}
}
